import {MeResponse} from "../apis/EmployeeApi";
import {NumberIdAndName} from "../types/BasicModels";
import {Authority} from "../apis/RoleApi";
import {makeAutoObservable} from "mobx";

export class EmployeeStore{
  employee: MeResponse | null = null

  constructor() {
    makeAutoObservable(this)
  }

  get employeeAsNumberIdAndName():NumberIdAndName|null{
    return this.employee?{
      id: this.employee.id,
      name: this.employee.name
    }:null
  }

  setEmployee(employee: MeResponse) {
    this.employee = employee
  }

  clear(){
    this.employee = null
  }

  hasAuthority(authority: Authority) {
    return !!this.employee?.authorities?.includes(authority)
  }

  hasAllAuthority(...authorities: Authority[]) {
    return authorities.every(authority => this.hasAuthority(authority))
  }

  hasAnyAuthority(...authorities: Authority[]) {
    return authorities.some(authority => this.hasAuthority(authority))
  }

}

export default new EmployeeStore()
